/*  Jarpa 1.05 - Java Packaging for Flash Lite Developers
 * 
 *  Recently, a new generation of mobile phones with support for Java ME and Flash Lite 
 *  has been taking widespread acceptance by the market, opening the door
 *  for hybrid applications. Looking for this business potential Jarpa was created
 *  allowing Flash Lite Developers deliver their content as Java ME applications.
 * 
 *  @author Felipe Andrade <felipe.andrade@i2tecnologia.com.br>
 * 
 *  http://www.i2tecnologia.com.br/jarpa/
 *  http://blog.felipeandrade.org/
 *  http://www.biskero.org
 */

package com.i2tecnologia.jarpa;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.ServerSocketConnection;
import javax.microedition.io.SocketConnection;

public class Connection implements Runnable {
    public static Connection _instance;    
    private ServerSocketConnection _server;
    private InputStream _in;
    private SocketConnection _client;
    public String _status = "not connected";
    public boolean _isConnected = false;
    private OutputStream _out;
    
    public static Connection getInstance() {
        if(_instance == null) {
            _instance = new Connection();
        }
        
        return _instance;
    }
    
    public Connection() {
        _status = "connecting...";
        new Thread(this).start();
    }
    
    public void write(String aData) throws Exception {
        _status = "sending data...";
        aData += '\0';
        
        synchronized (this) {
            if(_client != null) {
                //try {               
                   _out.write(aData.getBytes());
                   _out.flush();
                   //_out.close();
                /*} catch (Exception e) {
                    _status = "write: " + e.toString();
                }*/
            }
        }
    }
    
    public String read() {
        StringBuffer recv = new StringBuffer();
                
        synchronized (this) {
            if(_client != null) {
                try {
                    int ch;
                    if(_in.available() > 0) {
                         _status = "reading data...";
                        //while(( ch = _in.read()) != -1) {
                        while(( ch = _in.read()) != '\0') {
                            recv.append( (char) ch );
                        }
                    }
                } catch (Exception e) {
                    _status = "read: " + e.toString();
                }
            }
        }
        
        return recv.toString();
    }
    
    public void close() {
        try {
            _server.close();
            _client.close();
        } catch (IOException e) {
            _status = "close: " + e.toString();
        }
    }
    
    public void run() {
        try {
            _status = "connecting...";
            _server = (ServerSocketConnection) Connector.open("socket://:9002");
            _client = null;
        } catch (Exception e) {
            _status = "Run (1): " + e.toString();
        }
        
        try {
            _status = "waiting for incoming connection...";
            _client = (SocketConnection) _server.acceptAndOpen();
            _client.setSocketOption(SocketConnection.DELAY, 0);
            _client.setSocketOption(SocketConnection.KEEPALIVE, 2);
            
            _out = _client.openOutputStream();
            _in = _client.openInputStream();
            
            _status = "client connected...";
        } catch (Exception e) {
            _status = "Run (2): " + e.toString();
        }
    }
}
